<?php

namespace App\Service\Kms\User;

use App\Error\Kms\UserError;
use App\Models\Kms\User\KmsUserConfigModel;
use App\Service\BaseService;
use App\Service\OpenShop\Feishu\ConfigService;
use Exception;
use Illuminate\Validation\ValidationException;

class UserService extends BaseService
{
    /**
     * @return array|void
     * @throws Exception
     */
    public static function userinfo()
    {
        try {
            $userConfig  = KmsUserConfigModel::query()->where("userId", auth()->user()->rmsUserId)->first();
            $fieldConfig = ConfigService::getConfigDetail("kms.user", "attendance");
            return [
                "userinfo"    => auth()->user()->only([
                    "id",
                    "userId",
                    "rmsUserId",
                    "name",
                    "nickname",
                    "email",
                    "avatar",
                    "mobile",
                    "departmentName",
                    "rootDepartmentName",
                    "rootDepartmentId",
                    "jobTitle"
                ]),
                "fieldSwitch" => (bool)attributeGet($userConfig, "fieldSwitch", false),
                "fieldConfig" => [
                    "startTime" => attributeGet($fieldConfig, "value.field.startTimeFormat", ""),
                    "endTime"   => attributeGet($fieldConfig, "value.field.endTimeFormat", ""),
                ]
            ];
        } catch (Exception $exception) {
            handleKfException($exception, UserError::GET_USERINFO_FAIL);
        }
    }
}
